package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.<%=xia2Pasic(table.attr["@_name"])%>;
import priv.mill.server.<%=xia2Pasic(table.attr["@_name"])%>Service;

@Controller
@RequestMapping("<%=xia2Pasic(table.attr["@_name"])%>")
public class <%=xia2Pasic(table.attr["@_name"])%>Controller {

	@Autowired
	private <%=xia2Pasic(table.attr["@_name"])%>Service <%=xia2TuoFeng(table.attr["@_name"])%>Service;

	private static final Logger logger = Logger.getLogger(<%=xia2Pasic(table.attr["@_name"])%>Controller.class);

	@RequestMapping("get<%=xia2Pasic(table.attr["@_name"])%>ById")
	@ResponseBody
	public RetenObj get<%=xia2Pasic(table.attr["@_name"])%>ById(@RequestParam("<%=xia2Pasic(table.attr["@_name"])%>Id") Integer <%=xia2TuoFeng(table.attr["@_name"])%>Id) {
		try {
			return warpData(<%=xia2TuoFeng(table.attr["@_name"])%>Service.get<%=xia2Pasic(table.attr["@_name"])%>ById(<%=xia2TuoFeng(table.attr["@_name"])%>Id), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("get<%=xia2Pasic(table.attr["@_name"])%>ByEntry")
	@ResponseBody
	public RetenObj get<%=xia2Pasic(table.attr["@_name"])%>ByEntry(<%=xia2Pasic(table.attr["@_name"])%> <%=xia2TuoFeng(table.attr["@_name"])%>) {
		try {
			return warpData(<%=xia2TuoFeng(table.attr["@_name"])%>Service.get<%=xia2Pasic(table.attr["@_name"])%>ByEntry(<%=xia2TuoFeng(table.attr["@_name"])%>), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("get<%=xia2Pasic(table.attr["@_name"])%>sByPage")
	@ResponseBody
	public RetenObj get<%=xia2Pasic(table.attr["@_name"])%>sByPage(<%=xia2Pasic(table.attr["@_name"])%> <%=xia2TuoFeng(table.attr["@_name"])%>) {
		try {
			return warpData(<%=xia2TuoFeng(table.attr["@_name"])%>Service.get<%=xia2Pasic(table.attr["@_name"])%>sByPage(<%=xia2TuoFeng(table.attr["@_name"])%>), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("update<%=xia2Pasic(table.attr["@_name"])%>")
	@ResponseBody
	public RetenObj update<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2Pasic(table.attr["@_name"])%> <%=xia2TuoFeng(table.attr["@_name"])%>) {
		try {
			int result = <%=xia2TuoFeng(table.attr["@_name"])%>Service.update<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2TuoFeng(table.attr["@_name"])%>);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	<%
	var cuid=Array.isArray(table.id)?table.id[0]:table.id;
	%>

	@RequestMapping("insert<%=xia2Pasic(table.attr["@_name"])%>")
	@ResponseBody
	public RetenObj insert<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2Pasic(table.attr["@_name"])%> <%=xia2TuoFeng(table.attr["@_name"])%>) {
		try {
			int result = <%=xia2TuoFeng(table.attr["@_name"])%>Service.insert<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2TuoFeng(table.attr["@_name"])%>);
			if (result > 0)
				return warpData(<%=xia2TuoFeng(table.attr["@_name"])%>.get<%=xia2Pasic(cuid.attr["@_name"])%>(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("delete<%=xia2Pasic(table.attr["@_name"])%>")
	
	@ResponseBody
	public RetenObj delete<%=xia2Pasic(table.attr["@_name"])%>(Integer <%=xia2TuoFeng(table.attr["@_name"])%>Id) {
		try {
			int result = <%=xia2TuoFeng(table.attr["@_name"])%>Service.delete<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2TuoFeng(table.attr["@_name"])%>Id);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
